package main

import "fmt"

type Listnode struct {
	value int
	Next *Listnode
}

//反转链表
func reverse(head *Listnode) *Listnode {
	var prev *Listnode
	curr := head

	for curr != nil {
		next := curr.Next
		curr.Next = prev
		prev = curr
		curr = next
	}
	return prev
}

//打印链表
func printList(head *Listnode) {
	curr := head

	for curr != nil {
		fmt.Print(curr.value)
		curr = curr.Next
	}
	fmt.Println()
}

func main() {
	head := &Listnode{value:0}
	node1 := &Listnode{value:1}
	node2 := &Listnode{value:2}
	node3 := &Listnode{value:3}
	node4 := &Listnode{value:4}

	head.Next = node1
	node1.Next = node2
	node2.Next = node3
	node3.Next = node4

	fmt.Println("原始链表:")
	printList(head)

	newhead := reverse(head)
	fmt.Println("反转后的链表:")
	printList(newhead)
}
